package cn.edu.gdu.quizpractice.controller.web;

import cn.edu.gdu.quizpractice.entity.ApiResult;
import cn.edu.gdu.quizpractice.entity.Category;
import cn.edu.gdu.quizpractice.service.ICategoryService;
import io.micrometer.core.annotation.Timed;
import jakarta.websocket.server.PathParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Aubery
 * @Date: 2025-02-25-22:21
 * @Description:
 */
@RestController
@RequestMapping("/web/categories")
@Timed(value = "web/categoryController",description = "CategoryController")
@Slf4j
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/findByName/{name}")
    public ApiResult findByName(@PathVariable String name) {
        if (categoryService.findByName(name) == null)
            return ApiResult.fail("分类不存在");
        else
            return ApiResult.success(categoryService.findByName(name));
    }

    @PostMapping("/add")
    public ApiResult addCategory(@RequestParam String categoryName) {
        if (categoryService.addCateory(categoryName))
            return ApiResult.success("添加成功");
        else
            return ApiResult.fail("添加失败");
    }

    @DeleteMapping("/delete/{categoryName}")
    public ApiResult delete(@PathVariable String categoryName) {
        if (categoryService.deleteByName(categoryName))
            return ApiResult.success("删除成功");
        else
            return ApiResult.fail("删除失败");
    }

    @GetMapping("/findAll")
    public ApiResult findAll() {
        return ApiResult.success(categoryService.findAll());
    }

    @PostMapping("/update")
    public ApiResult update(@RequestBody Category category) {
        if (categoryService.updateCategory(category) > 0)
            return ApiResult.success("更新成功");
        else
            return ApiResult.fail("更新失败");
    }

    /**
     * 根据分类名称查找题库
     * @param categoryName
     * @return
     */
    @GetMapping("/findBanksByCategoryName")
    public ApiResult findBanksByCategoryName(@RequestParam("categoryName") String categoryName) {
        return ApiResult.success(categoryService.findBanksByCategoryName(categoryName));
    }

}
